/**
 * 获取图片元素主体颜色
 * 获取图片像素矩阵, 通过像素计算主体颜色
 */
export function getMainColor(src: string): string {
    return '';
}

const qulity = 100, count = 0;

/**
 * 获取主题色
 */
function getColor(cube) {
    let maxr = cube[0][0],
        minr = cube[0][0],
        maxg = cube[0][1],
        ming = cube[0][1],
        maxb = cube[0][2],
        minb = cube[0][2];
    for (let i = 0; i < cube.length; i++) {
        if (cube[i][0] > maxr) {
            maxr = cube[i][0];
        }
        if (cube[i][0] < minr) {
            minr = cube[i][0];
        }
        if (cube[i][1] > maxg) {
            maxg = cube[i][1];
        }
        if (cube[i][1] < ming) {
            ming = cube[i][1];
        }
        if (cube[i][2] > maxb) {
            maxb = cube[i][2];
        }
        if (cube[i][2] < minb) {
            minb = cube[i][2];
        }
    }

    if ((maxr - minr) < qulity && (maxg - ming) < qulity && (maxb - minb) < qulity) {
        let r = 0, g = 0, b = 0;
        for (let i = 0; i < cube.length; i++) {
            r += cube[i][0];
            g += cube[i][1];
            b += cube[i][2];
        }
        let divcolor = document.createElement("div");
        divcolor.style.backgroundColor = 'rgba(' + r / (cube.length) + ',' + g / (cube.length) + ',' + b / (cube.length) + ')';
        divcolor.style.width = '100px';
        divcolor.style.height = '100px';
        let html = document.getElementById('imagecolor');
        html.appendChild(divcolor);
    } else {
        let maxrgb = 0;
        let rgbindex = 0;
        let rgbmiddle = 0;

        if ((maxr - minr) > maxrgb) {
            maxrgb = (maxr - minr);
            rgbmiddle = (maxr + minr) / 2
            rgbindex = 0;
        }
        if ((maxg - ming) > maxrgb) {
            maxrgb = (maxg - ming);
            rgbmiddle = (maxg + ming) / 2;
            rgbindex = 1;
        }
        if ((maxb - minb) > maxrgb) {
            maxrgb = (maxb - minb);
            rgbmiddle = (maxb + minb) / 2;
            rgbindex = 2;
        }

        // 排序
        cube.sort(function (x, y) {
            return x[rgbindex] - y[rgbindex];
        });
        let cubea = new Array();
        let cubeb = new Array();
        for (let i = 0; i < cube.length; i++) {
            if (cube[i][rgbindex] < rgbmiddle) {
                cubea.push(cube[i]);
            } else {
                cubeb.push(cube[i]);
            }
        }

        getColor(cubeb);
        getColor(cubea);
    }
}
